﻿using System;
using System.ComponentModel;
using System.Diagnostics.Contracts;

namespace TrackIT.Common
{
    public class Task : INotifyPropertyChanged
    {
        #region Properties

        private string _name;
        private string _description;
        private DateTime _recuringTime;

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                _description = value;
                OnPropertyChanged("Description");
            }
        }

        public DateTime RecuringTime
        {
            get
            {
                return _recuringTime;
            }
            set
            {
                _recuringTime = value;
                OnPropertyChanged("RecuringTime");
            }
        }

        #endregion Properties

        public event PropertyChangedEventHandler PropertyChanged;

        public Task()
        {
            Name = "";
            Description = "";
        }

        public Task(string task)
        {
            Contract.Requires(!string.IsNullOrEmpty(task));

            if (!task.Contains("|"))
            {
                Name = task;
                Description = "";
            }
            else
            {
                string[] deserialized = task.Split('|');
                if (deserialized.Length >= 2)
                {
                    Name = deserialized[0];
                    Description = deserialized[1];
                    if (deserialized.Length == 3)
                    {
                        string timeStr = deserialized[2];
                        string[] timeSplit = timeStr.Split(':');
                        if (timeSplit.Length == 2)
                        {
                            string hours = timeSplit[0];
                            string minutes = timeSplit[1];
                            int hour, min;
                            bool result = Int32.TryParse(hours, out hour);
                            result = Int32.TryParse(minutes, out min) && result;

                            if (result && 
                                hour >= 0 && hour < 24 &&
                                min >= 0 && min < 60 )
                            {
                                _recuringTime = new DateTime(1, 1, 1, hour, min, 0);
                            }
                        }
                    }
                }
            }
        }

        public Task(string name, string description)
        {
            Name = name;
            Description = description;
        }

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public override bool Equals(object obj)
        {
            Task comp = obj as Task;
            if (comp == null)
            {
                return base.Equals(obj);
            }
            return comp.Name.Equals(Name);
        }

        public override int GetHashCode()
        {
            return _name.GetHashCode();
        }
    }
}